package com.sunny.ihealth;

import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.jaeger.library.StatusBarUtil;

public class MainActivity extends BaseActivity implements NavigationView.OnNavigationItemSelectedListener {

    private DrawerLayout drawerLayout;
    private ActionBarDrawerToggle drawerToggle;
    private NavigationView navigationView;
    private Toolbar toolbar;
    private LinearLayout llToolbarTabs;

    //按下返回键的时间
    private long pressBackTime = 0L;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initViews();

    }

    private void initViews() {
        setToolbar("");

        drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout_main);
        navigationView = (NavigationView) findViewById(R.id.navigation_main);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        llToolbarTabs = (LinearLayout) findViewById(R.id.ll_toolbar_tabs);

        //透明状态栏
        StatusBarUtil.setColorForDrawerLayout2(this, drawerLayout, getResources().getColor(R.color.colorPrimaryDark),
                getResources().getInteger(R.integer.status_alpha));

        //汉堡图标
        drawerToggle = new ActionBarDrawerToggle(MainActivity.this, drawerLayout, getToolbar(), R.string.drawer_open, R.string.drawer_close);
        drawerToggle.syncState();

        //显示3个在Toolbar上的按钮
        llToolbarTabs.setVisibility(View.VISIBLE);
        navigationView.setNavigationItemSelectedListener(this);

    }

    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
                    drawerLayout.closeDrawer(GravityCompat.START);
                } else {
                    drawerLayout.openDrawer(GravityCompat.START);
                }
        }
        return super.onOptionsItemSelected(item);
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
                drawerLayout.closeDrawer(GravityCompat.START);
                return true;
            }
            long time = System.currentTimeMillis();
            if (time - pressBackTime > 2000) {
                pressBackTime = time;
                Toast.makeText(MainActivity.this, "再按一次退出", Toast.LENGTH_SHORT).show();
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
